package net.cathackers.devel.scmp.chat.interfaces;

import java.util.Date;
import java.util.Random;
import java.util.Set;

import org.dom4j.Document;

import net.cathackers.devel.scmp.auth.interfaces.UserID;
import net.cathackers.devel.scmp.auth.interfaces.UserProfile;
import net.cathackers.devel.scmp.chat.impl.xmpp.iqcomponent.interfaces.PacketRouter;
import net.cathackers.devel.scmp.interfaces.ServiceManager;
import net.cathackers.devel.scmp.interfaces.ServiceType;
import net.cathackers.devel.scmp.xmpp.Packet;

public abstract class XMPPChatManager implements ServiceManager, PacketRouter, ChatManager {

    private static String __GUID__ = "f82ec274-a7e9-49c5-962e-dfb9d69d8771";
    protected static char[] numbersAndLetters_ = ("0123456789abcdefghijklmnopqrstuvwxyz"
	    + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
    protected static Random random_ = new Random(new Date().getTime());
    protected ManagedQueue<Packet> ouputQueue_;

    public static synchronized String generateRandomString(int length) {
	if (length < 1) {
	    return null;
	}
	// Create a char buffer to put random letters and numbers in.
	char[] randBuffer = new char[length];
	for (int i = 0; i < randBuffer.length; i++) {
	    randBuffer[i] = numbersAndLetters_[random_.nextInt(71)];
	}
	return new String(randBuffer);
    }

    /* (non-Javadoc)
     * @see net.cathackers.devel.scmp.chat.interfaces.ChatManager#getGUID()
     */
    @Override
    public String getGUID() {
	return __GUID__;
    }

    /* (non-Javadoc)
     * @see net.cathackers.devel.scmp.chat.interfaces.ChatManager#getUserProfile(net.cathackers.devel.scmp.auth.interfaces.UserID)
     */
    public abstract UserProfile getUserProfile(UserID userID);

    /* (non-Javadoc)
     * @see net.cathackers.devel.scmp.chat.interfaces.ChatManager#getServiceType()
     */
    @Override
    public ServiceType getServiceType() {
	return ServiceType.CHAT;
    }

    /* (non-Javadoc)
     * @see net.cathackers.devel.scmp.chat.interfaces.ChatManager#generateSessionID()
     */
    public abstract String generateSessionID();

    /* (non-Javadoc)
     * @see net.cathackers.devel.scmp.chat.interfaces.ChatManager#registerSession(net.cathackers.devel.scmp.chat.interfaces.ChatSession)
     */
    public abstract void registerSession(ChatSession session);

    /* (non-Javadoc)
     * @see net.cathackers.devel.scmp.chat.interfaces.ChatManager#unregisterSession(net.cathackers.devel.scmp.chat.interfaces.ChatSession)
     */
    public abstract boolean unregisterSession(ChatSession session);

    /* (non-Javadoc)
     * @see net.cathackers.devel.scmp.chat.interfaces.ChatManager#getName()
     */
    @Override
    public String getName() {
	return "Chat";
    }

    void deliver(Packet p) {
	ouputQueue_.enqueue(p);
    }

    /* (non-Javadoc)
     * @see net.cathackers.devel.scmp.chat.interfaces.ChatManager#createSession(java.lang.String, org.dom4j.Document, net.cathackers.devel.scmp.chat.interfaces.XMPPConnection)
     */
    public abstract ChatSession createSession(String serverName,
	    Document initDoc, XMPPConnection connection);

    /* (non-Javadoc)
     * @see net.cathackers.devel.scmp.chat.interfaces.ChatManager#destroySession(java.lang.String)
     */
    public abstract boolean destroySession(String sessionID);

    /* (non-Javadoc)
     * @see net.cathackers.devel.scmp.chat.interfaces.ChatManager#deliverToUser(net.cathackers.devel.scmp.xmpp.Packet)
     */
    public abstract void deliverToUser(Packet packet);
    
    /* (non-Javadoc)
     * @see net.cathackers.devel.scmp.chat.interfaces.ChatManager#getChatSession(net.cathackers.devel.scmp.auth.interfaces.UserID)
     */
    public abstract ChatSession[] getChatSession(UserID userID);
    
    /* (non-Javadoc)
     * @see net.cathackers.devel.scmp.chat.interfaces.ChatManager#getChatSession(java.lang.String)
     */
    public abstract ChatSession getChatSession(String sessionID);
    
    /* (non-Javadoc)
     * @see net.cathackers.devel.scmp.chat.interfaces.ChatManager#checkIfSessionOnline(net.cathackers.devel.scmp.auth.interfaces.UserID)
     */
    public abstract boolean checkIfSessionOnline(UserID userID);
    
    /* (non-Javadoc)
     * @see net.cathackers.devel.scmp.chat.interfaces.ChatManager#identifySession(java.lang.String)
     */
    public abstract void identifySession(String sessionID);
    
    public abstract void addSubscriber(UserID owner, UserID subscriber);
    
    public abstract void removeSubscriber(UserID owner, UserID subscriber);
    
    public abstract Set<UserID> getSubscribers(UserID owner);
    
    public abstract void removeSubscribersCacheElement(UserID owner);

}
